package lruCache;

import java.util.HashMap;

//注意 ： 这里容易栈溢出  每次调整位置，并为真正，删除节点
public class LRUCache {
    //双向链表
    class DLinkedNode{
        int key;
        int val;
        DLinkedNode prev;
        DLinkedNode next;
        public DLinkedNode(int key,int val){
            this.key = key;
            this.val = val;
        }
        public DLinkedNode(){
        }

        @Override
        public String toString() {
            return "{" + "key=" + key + ", val=" + val + "}";
        }
    }
    int useSize;
    int capacity;
    DLinkedNode head;
    DLinkedNode tail;
    HashMap<Integer,DLinkedNode> map;

    public LRUCache(){
        this.capacity = 3;
        this.map = new HashMap<>();
        this.head = new DLinkedNode();
        this.tail = new DLinkedNode();
        this.head.next = tail;
        this.tail.prev = head;
    }

    public LRUCache(int capacity){
        this.capacity = capacity;
        this.map = new HashMap<>();
        this.head = new DLinkedNode();
        this.tail = new DLinkedNode();
        this.head.next = tail;
        this.tail.prev = head;
    }


    public boolean put(int key,int val){
        //1.当useSize == 0 时
        if(useSize == 0){
            //进行头插
            DLinkedNode node = new DLinkedNode(key,val);
            map.put(key,node);
            addHandNode(node);
            useSize++;
            print();
            return true;
        }
        //看是否存在key
        DLinkedNode node = map.get(key);
        if(node == null){
            //进行尾插
            node = new DLinkedNode(key,val);
            map.put(key,node);
            addTailNode(node);
            useSize++;
            while(useSize > capacity){
                // 删除第一个节点
                map.put(head.next.key,null);
                deleteNode(head.next);

                useSize--;
            }
        }else {
            if(node.val != val){
                node.val = val;
            }
            //1.先删除节点
            deleteNode(node);
            //2.进行尾插
            addTailNode(node);
        }
        print();
        return true;
    }

    //头插
    private void addHandNode(DLinkedNode node){
        DLinkedNode hN = head.next;
        node.prev = head;
        node.next = hN;
        head.next = node;
        hN.prev = node;
    }

    //尾插
    private void addTailNode(DLinkedNode node){
        DLinkedNode tP = tail.prev;
        node.next = tail;
        tail.prev = node;
        node.prev = tP;
        tP.next = node;
    }

    //删除当前节点
    private void deleteNode(DLinkedNode node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    public int get(int key) {
        DLinkedNode node = map.get(key);
        if(node == null){
            return -1;
        }
        deleteNode(node);
        addTailNode(node);
        print();
        return node.val;
    }

    //进行打印
    public void print(){
        DLinkedNode cur = head.next;
        while(cur != tail){
            System.out.print(cur);
            cur = cur.next;
        }
        System.out.println();
    }

}
